package cn.com.zero.manager.auth.controller;

import cn.com.zero.api.auth.vo.OnlineUserVO;
import cn.com.zero.db.vo.PageParamsVO;
import cn.com.zero.db.vo.PageVO;
import cn.com.zero.manager.auth.constant.ManagerAuthConstants;
import cn.com.zero.manager.auth.service.impl.ManagerOnlineUserService;
import cn.com.zero.web.util.PageRequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * @author guoxin
 * @version 1.0.0
 * @Description
 * @date 2023/4/3 15:52
 */
@Api(tags = "在线管理员管理接口")
@Validated
@RestController
@RequestMapping("/manager/managerOnline")
public class ManagerOnlineUserController {

    /**
     * 在线管理员管理service
     */
    private ManagerOnlineUserService managerOnlineUserService;

    @ApiOperation("查询在线管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "searchUserIds", value = "用户编号集合"),
            @ApiImplicitParam(paramType = "query", name = "loginDevice", value = "登录设备：1：pc-web 2：android 3：ios 4：mobile-web"),
            @ApiImplicitParam(paramType = "query", name = "loginIp", value = "登录ip地址"),
            @ApiImplicitParam(paramType = "query", name = "loginBeginTime", value = "开始时间;格式:yyyy-MM-dd"),
            @ApiImplicitParam(paramType = "query", name = "loginEndTime", value = "结束时间;格式:yyyy-MM-dd")
    })
    @GetMapping("/list")
    public PageVO<OnlineUserVO> list(@RequestParam(value = "searchUserIds", required = false) String[] searchUserIds,
                                     @RequestParam(value = "loginDevice", required = false) String loginDevice,
                                     @RequestParam(value = "loginIp", required = false) String loginIp,
                                     @RequestParam(value = "loginBeginTime", required = false) Date loginBeginTime,
                                     @RequestParam(value = "loginEndTime", required = false) Date loginEndTime) {
        PageParamsVO pageParamsVO = PageRequestUtil.getPageParams();
        return managerOnlineUserService.getOnlineUsers(ManagerAuthConstants.MANAGER_SIDE_NAME,pageParamsVO,
                null == searchUserIds ? null : Arrays.asList(searchUserIds), loginDevice, loginIp, loginBeginTime, loginEndTime);
    }

    @ApiOperation(value = "下线管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "userId", value = "用户Id，当传入用户Id未登录时返回 false，已登录，且强制下线成功返回 true"),
            @ApiImplicitParam(paramType = "query", name = "loginDevice", value = "登录设备：1：pc-web 2：android 3：ios 4：mobile-web")
    })
    @PostMapping("/kickoff/{userId}")
    public void kickoff(@PathVariable("userId") String userId,
                        @RequestParam(value = "loginDevice") String loginDevice){
        managerOnlineUserService.kickOffUser(ManagerAuthConstants.MANAGER_SIDE_NAME,
                userId, loginDevice);
    }

    @Autowired
    public void setManagerOnlineUserService(ManagerOnlineUserService managerOnlineUserService) {
        this.managerOnlineUserService = managerOnlineUserService;
    }
}
